

' clase que utilizamos a la hora de leer de la base 
' de datos para evitar problemas

Public Class sf
   
    Public Shared Function SafeSql( _
ByVal inputSQL As String) As String
        Dim s As String = inputSQL
        s = inputSQL.Replace("'", "''")
        s = s.Replace("[", "[[]")
        s = s.Replace("%", "[%]")
        s = s.Replace("_", "[_]")
        Return s
    End Function


#Region "PreparaObjeto"
    ' esta funcion arregla los datos para que no haya ningun null
    ' util a la hora de hacer un update
    Public Function preparaObjeto(ByVal objeto As Object)
        Try
            Dim contador As Integer = 0
            Dim pi As System.Reflection.PropertyInfo

            For Each pi In objeto.GetType().GetProperties()

                If (pi.CanWrite) Then

                    Select Case pi.PropertyType.ToString
                        Case "System.String"
                            If pi.GetValue(Me, Nothing) Is Nothing Then
                                pi.SetValue(Me, "", Nothing)
                            End If
                        Case "System.Boolean"
                            If pi.GetValue(Me, Nothing) Is Nothing Then
                                pi.SetValue(Me, False, Nothing)
                            End If
                        Case "System.int32"
                            If pi.GetValue(Me, Nothing) Is Nothing Then
                                pi.SetValue(Me, 0, Nothing)
                            End If
                        Case "System.int64"
                            If pi.GetValue(Me, Nothing) Is Nothing Then
                                pi.SetValue(Me, 0, Nothing)
                            End If

                        Case "System.Double"
                            If pi.GetValue(Me, Nothing) Is Nothing Then
                                pi.SetValue(Me, 0, Nothing)
                            End If

                        Case "System.single"
                            If pi.GetValue(Me, Nothing) Is Nothing Then
                                pi.SetValue(Me, 0.0, Nothing)
                            End If

                        Case "System.Date"
                            If pi.GetValue(Me, Nothing) Is Nothing Then
                                pi.SetValue(Me, Date.MinValue, Nothing)
                            End If

                    End Select


                End If
            Next
        Catch ex As Exception
        End Try

    End Function
#End Region

#Region "Single"
    Public Shared Function aSingle(ByVal valor As String) As Single
        Try
            If valor = "" Then
                Return 0.0 'Single.MinValue
            Else
                'valor = valor.Replace(".", "")
                'Return System.Convert.ToSingle(valor)
                Return CSng(valor)
            End If
        Catch ex As Exception
            Return 0.0 'Single.MinValue
        End Try

    End Function

    Public Shared Function aSingle(ByVal valor As Integer) As Single
        Try
            If valor = 0 Then
                Return 0.0  'Single.MinValue
            Else
                Return CSng(valor)
            End If
        Catch ex As Exception
            Return 0.0 'Single.MinValue
        End Try
    End Function


    Public Shared Function aSingle(ByVal valor As Single) As Single
        If valor = Single.MinValue Then
            Return 0
        Else
            Return (valor)
        End If
    End Function


#End Region



#Region "Doble"

    Public Shared Function Doble(ByVal valor As Double) As Double
        Try
            If valor = 0 Then
                Return 0
            Else
                Return valor
            End If
        Catch ex As Exception
            Return 0
        End Try

    End Function

    Public Shared Function Doble(ByVal valor As String) As Double
        Try
            If valor = "" Then
                Return 0
            Else
                'valor = valor.Replace(".", ",")
                'Return System.Convert.ToDouble(valor)
                Return CDbl(valor)
            End If
        Catch ex As Exception
            Return 0
        End Try

    End Function

    Public Shared Function Doble(ByVal valor As Boolean) As Double
        Try
            If valor = True Then
                Return 1
            Else
                Return 0
            End If
        Catch ex As Exception
            Return 0
        End Try

    End Function
#End Region


#Region "Cadena"
    Public Shared Function Cadena(ByVal valor As Double) As String
        Try
            If valor = 0 Then
                Return "0"
            Else
                Return valor.ToString
            End If
        Catch ex As Exception
            Return 0
        End Try

    End Function

    Public Shared Function Cadena(ByVal valor As String) As String
        Try
            Return valor
        Catch ex As Exception
            Return ""
        End Try

    End Function


    Public Shared Function Cadena(ByVal valor As Integer) As String
        Try
            Return CStr(valor)
        Catch ex As Exception
            Return ""
        End Try

    End Function
    Public Shared Function Cadena(ByVal valor As Boolean) As String
        Try
            If valor = True Then
                Return "1"
            Else
                Return "0"
            End If
        Catch ex As Exception
            Return "0"
        End Try

    End Function


    Public Shared Function Cadena(ByVal valor As System.DBNull) As String
        Try
            Return ""
        Catch ex As Exception
            Return ""
        End Try

    End Function

    'Public Shared Function Cadena(ByVal valor As Object) As String
    '    Try
    '        Return CStr(Cadena)
    '    Catch ex As Exception
    '        Return ""
    '    End Try

    'End Function
#End Region


#Region "Money"

    ' comprueba que una cadena no tiene .
    Public Shared Function Money(ByVal valor As String) As String
        Try
            If valor = "" Then
                Return "0"
            Else
                valor = valor.Replace(".", ",")
                Return valor
            End If
        Catch ex As Exception
            Return 0
        End Try

    End Function


#End Region


    '' transforma una cadena  a double
    'Public Shared Function string2Double(ByVal valor As String) As Double
    '    Try
    '        If valor = "" Then
    '            Return 0
    '        Else
    '            valor = valor.Replace(".", "")
    '            Return System.Convert.ToDouble(valor)
    '        End If
    '    Catch ex As Exception
    '        Return 0
    '    End Try

    'End Function

    '' transforma un doble a cadena, util para sentencias sql
    'Public Shared Function double2Cadena(ByVal valor As Double) As String
    '    Try
    '        Return valor.ToString.Replace(".", ",")
    '    Catch ex As Exception
    '        Return "0"
    '    End Try

    'End Function

    '' transforma un single a cadena, util para sentencias sql
    'Public Shared Function single2Cadena(ByVal valor As Single) As String
    '    Try
    '        Return valor.ToString.Replace(",", ".")
    '    Catch ex As Exception
    '        Return "0"
    '    End Try

    'End Function

#Region "Entero"
    Public Shared Function Entero(ByVal valor As String) As Integer
        Try
            If valor = "" Then
                Return 0
            Else
                'valor = valor.Replace(",", ".")

                ' le quitamos los decimales para evitarnos sustos
                'If valor.IndexOf(".") <> -1 Then
                'valor = valor.Substring(0, valor.IndexOf("."))
                'End If

                'Return System.Convert.ToInt64(valor)
                Return CInt(valor)
            End If
        Catch ex As Exception
            Return 0
        End Try

    End Function

    Public Shared Function Entero(ByVal valor As Double) As Integer
        Try
            If valor = 0 Then
                Return 0
            Else
                ' valor = valor.Replace(",", ".")
                'Return System.Convert.ToInt64(valor)
                Return CInt(valor)
            End If
        Catch ex As Exception
            Return 0
        End Try

    End Function

    Public Shared Function Entero(ByVal valor As Single) As Integer
        Try
            If valor = 0 Then
                Return 0
            Else
                ' valor = valor.Replace(",", ".")
                'Return System.Convert.ToInt64(valor)
                Return CInt(valor)
            End If
        Catch ex As Exception
            Return 0
        End Try

    End Function

    Public Shared Function Entero(ByVal valor As Boolean) As Integer
        Try
            If valor = True Then
                Return 1
            Else
                Return 0
            End If
        Catch ex As Exception
            Return 0
        End Try

    End Function

    Public Shared Function Entero(ByVal valor As System.DBNull) As Integer
        Try
            Return 0
        Catch ex As Exception
            Return 0
        End Try

    End Function
#End Region


#Region "Fecha"
    Public Shared Function Fecha(ByVal valor As String) As Date
        Try
            If valor = "" Then
                Return Date.MinValue
            Else
                'Return System.Convert.ToDateTime(valor)
                Return CDate(valor)
            End If
        Catch ex As Exception
            Return Date.MinValue
        End Try
    End Function
#End Region




    Public Shared Function Bool(ByVal valor As String) As Boolean
        Try
            Select Case valor
                Case "0", "NO"
                    Return False
                Case "1", "TRUE", "YES"
                    Return True
            End Select


            Return CBool(valor)

        Catch ex As Exception
            Return False
        End Try

    End Function

    Public Shared Function Bool(ByVal valor As System.DBNull) As Boolean
        Try
            Return False
        Catch ex As Exception
            Return False
        End Try

    End Function

End Class
